package org.example;

import org.apache.zookeeper.KeeperException;

import java.io.IOException;

public class DistributedLockMain {
    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        DistributedLock lock1 = new DistributedLock();
        DistributedLock lock2 = new DistributedLock();

        new Thread(() -> {

            try {
                lock1.lock();
                System.out.println("1获取锁");
                Thread.sleep(1000);
                lock1.unlock();
                System.out.println("1解锁");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }).start();

        new Thread(() -> {

            try {
                lock2.lock();
                System.out.println("2获取锁");
                Thread.sleep(5 * 1000);
                lock2.unlock();
                System.out.println("2解锁");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }).start();
    }
}

